unit clsRepresQuota;

interface

uses Dm, dbtables, biblio;

type
  TRepresQuota = class(TObject)
  private
    Rep_Codigo: string;
  public
    Ano_Mes: string;
    Valor: Double;
    function Gravar(p_Rep_codigo, p_Ano_Mes: string): Byte;
    function Recuperar(p_Rep_codigo, p_Ano_Mes: string): Byte;
    function Excluir(p_Rep_codigo, p_Ano_Mes: string): Byte;
    function Excluir_Tudo(p_Rep_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_Rep_codigo: string);
    function Percentual_Atingido(p_Rep_codigo, p_Ano_Mes: string; vlr: Double): Double;
  end;

implementation

{ TRepresQuota }

procedure TRepresQuota.Consultar(var p_q: TQuery; p_Rep_codigo: string);
begin
  p_q.active := False;
  p_q.sql.clear;
  p_q.params.clear;
  p_q.sql.add('SELECT * FROM REPRESQUOTA where Rep_Codigo =:k_Rep');
  p_q.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  p_q.open;
end;

function TRepresQuota.Excluir(p_Rep_codigo, p_Ano_Mes: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete FROM REPRESQUOTA where Rep_Codigo =:k_Rep and Ano_Mes =:k_Data');
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.ParamByName('k_Data').AsString := p_Ano_Mes;
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.Free;
end;

function TRepresQuota.Excluir_Tudo(p_Rep_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete FROM REPRESQUOTA where Rep_Codigo =:k_Rep');
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.Free;
end;

function TRepresQuota.Gravar(p_Rep_codigo, p_Ano_Mes: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM REPRESQUOTA where Rep_Codigo =:k_Rep and Ano_Mes =:k_Data');
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.ParamByName('k_Data').AsString := p_Ano_Mes;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO REPRESQUOTA VALUES(:k_Rep,:k_Data,:k_Valor)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE REPRESQUOTA SET Valor =:K_Valor WHERE Rep_Codigo =:k_Rep and Ano_Mes =:k_Data');
  end;
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.ParamByName('k_Data').AsString := p_Ano_Mes;
  tq.parambyname('k_Valor').AsFloat := Valor;
  tq.close;
  tq.execsql;
  result := 1;
  tq.Free;
end;

function TRepresQuota.Percentual_Atingido;
var
  tq: TQuery;
  Perc: Double;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM REPRESQUOTA where Rep_Codigo =:k_Rep and Ano_Mes =:k_Data');
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.ParamByName('k_Data').AsString := p_Ano_Mes;
  tq.open;
  if tq.eof then begin
    Valor := 0;
    Result := 0;
  end else begin
    Valor := tq.FieldByName('Valor').AsFloat;
    if Valor = 0 then Valor := 1;
    Perc := (100 * vlr) / Valor;
    Result := Perc;
  end;
  tq.Close;
  tq.Free;
end;

function TRepresQuota.Recuperar(p_Rep_codigo, p_Ano_Mes: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM REPRESQUOTA where Rep_Codigo =:k_Rep and Ano_Mes =:k_Data');
  tq.parambyname('k_Rep').asstring := strzero(p_Rep_codigo, 3);
  tq.ParamByName('k_Data').AsString := p_Ano_Mes;
  tq.open;
  if tq.eof then begin
    Valor := 0;
    Result := 0;
  end else begin
    Valor := tq.FieldByName('Valor').AsFloat;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

end.
